package com.cy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Controller
public class HealthController {
    private HashMap<String,Object> map = new HashMap<>();

    @RequestMapping("/doHealthUI")
    public String doHealthUI(Model model){

//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("default.html");
//        modelAndView.addObject("username","张三");
//        modelAndView.addObject("state","亚健康");


        model.addAttribute("username","张三");
        model.addAttribute("state","健康");

        return "default";
    }

    @RequestMapping("/health.html")
    @ResponseBody//返回值尽量是特定格式进行转换（例如JSON）
    //将转化的结果写到Response对象中
    //方法的返回值不再封装为ModelAndView对象，直接把response对象返回
    public HashMap<String, Object> doHealth(){
        map.put("username","张三");
        map.put("state",true);
        return map;
    }
    @RequestMapping("/doPrint")
    @ResponseBody
    public void doPrint(HttpServletResponse resp){
        try {
            System.out.println("********");
            resp.getWriter().println("********");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
